﻿/******************************************************************************

    Copyright (c) 2010, Sergey Mudrov
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
          notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
          notice, this list of conditions and the following disclaimer in the
          documentation and/or other materials provided with the distribution.
        * Neither the name of Sergey Mudrov nor the
          names of its contributors may be used to endorse or promote products
          derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL SERGEY MUDROV BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VkApi.DataTypes;
using System.Collections.Generic;
using System.Windows.Browser;

namespace VkApi
{
    /// <summary>
    /// Class that represents parameters initialization.
    /// </summary>
    public class InitialParameters
    {
        private Dictionary<string, string> _urlparams = null;

        /// <summary>
        /// Gets or sets the application ID.
        /// </summary>
        /// <value>The application ID.</value>
        public int ApplicationID { get; protected set; } // api_id

        /// <summary>
        /// Gets or sets the application settings.
        /// </summary>
        /// <value>The application settings.</value>
        public UserSettings ApplicationSettings { get; protected set; } // api_settings

        /// <summary>
        /// Gets or sets the viewer ID.
        /// </summary>
        /// <value>The viewer ID.</value>
        public int ViewerID { get; protected set; } // viewer_id

        /// <summary>
        /// Gets or sets the type of the viewer.
        /// </summary>
        /// <value>The type of the viewer.</value>
        public int ViewerType { get; protected set; } // viewer_type

        /// <summary>
        /// Gets or sets the user ID.
        /// </summary>
        /// <value>The user ID.</value>
        public int UserID { get; protected set; } // user_id

        /// <summary>
        /// Gets or sets the group ID.
        /// </summary>
        /// <value>The group ID.</value>
        public int GroupID { get; protected set; } // group_id

        /// <summary>
        /// Gets or sets a value indicating whether this instance is application user.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is application user; otherwise, <c>false</c>.
        /// </value>
        public bool IsApplicationUser { get; protected set; } // is_app_user

        /// <summary>
        /// Gets or sets the authorization key.
        /// </summary>
        /// <value>The authorization key.</value>
        public string AuthKey { get; protected set; } // auth_key

        /// <summary>
        /// Gets or sets the language.
        /// </summary>
        /// <value>The language.</value>
        public int Language { get; protected set; } // language

        /// <summary>
        /// Gets or sets the parent language.
        /// </summary>
        /// <value>The parent language.</value>
        public int ParentLanguage { get; protected set; } // parent_language

        /// <summary>
        /// Gets or sets the referrer.
        /// </summary>
        /// <value>The referrer.</value>
        public string Referrer { get; protected set; } // referrer

        /// <summary>
        /// Gets or sets the name of the local name.
        /// </summary>
        /// <value>The local name.</value>
        public string LcName { get; protected set; } // lc_name

        /// <summary>
        /// Initializes a new instance of the <see cref="InitialParameters"/> class.
        /// </summary>
        public InitialParameters()
        {
#if DEBUG
            Trace.WriteLine("InitialParameters: Init parameters");
#endif
            this._urlparams = HtmlPage.Document.QueryString as Dictionary<string, string>;

            this.ApplicationID = this.GetIntValue("api_id"); 
            this.ApplicationSettings = (UserSettings)this.GetIntValue("api_settings");
            this.ViewerID = this.GetIntValue("viewer_id");
            this.ViewerType = this.GetIntValue("viewer_type");
            this.UserID = this.GetIntValue("user_id");
            this.GroupID = this.GetIntValue("group_id");
            this.IsApplicationUser = this.GetIntValue("is_app_user") == 1;
            this.AuthKey = (this._urlparams.ContainsKey("auth_key")) ? this._urlparams["auth_key"] : "";
            this.Language = this.GetIntValue("language");
            this.ParentLanguage = this.GetIntValue("parent_language");
            this.Referrer = (this._urlparams.ContainsKey("referrer")) ? this._urlparams["referrer"] : "";
            this.LcName = (this._urlparams.ContainsKey("lc_name")) ? this._urlparams["lc_name"] : "";

#if DEBUG
            Trace.WriteLine("InitialParameters: Init parameters succeeded");
#endif
        }

        /// <summary>
        /// 
        /// </summary>
        private int GetIntValue(string name)
        {
            int v = 0;

            if (this._urlparams.ContainsKey(name) == false)
                return 0;

            Int32.TryParse(this._urlparams[name], out v);

            return v;
        }
    }
}
